<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class m_jenis extends CI_Model {

    function tambah() {
        $kode = $this->input->post('kd_soal');
        $jenis = $this->input->post('jn_soal');
        $nama = $this->input->post('nm_soal');
        $versi = $this->input->post('versi');
        $data = array('id_jenis' => NULL,
            'kode_jenis' => $kode,
            'nama_jenis' => $nama,
            'jenis_soal' => $jenis,
            'versi_soal' => $versi);
        $this->db->insert('tb_jenis', $data);
    }

    function ambil($limit, $offset) {
        $this->db->order_by('id_jenis', 'DESC');
        $ambil = $this->db->get('tb_jenis', $limit, $offset);
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        }
    }

    function jml_soal($a) {
        $query = $this->db->get_where('tb_soal', array('id_jenis' => $a));
        $jml = $query->num_rows();
        return $jml;
    }

    function edit($a) {
        $d = $this->db->get_where('tb_jenis', array('id_jenis' => $a))->row();
        return $d;
    }

    function hapus($a) {
        $b = $this->db->get_where('tb_soal', array('id_jenis' => $a))->row();
        $idsoal = $b->id_soal;
        $this->db->delete('tb_jawaban', array('id_soal' => $idsoal));
        $this->db->delete('tb_soal', array('id_jenis' => $a));
        $this->db->delete('tb_jenis', array('id_jenis' => $a));
        return;
    }

    function update($id) {
        $kode = $this->input->post('kd_soal');
        $jenis = $this->input->post('jn_soal');
        $nama = $this->input->post('nm_soal');
        $versi = $this->input->post('versi');
        $data = array(
            'kode_jenis' => $kode,
            'nama_jenis' => $nama,
            'jenis_soal' => $jenis,
            'versi_soal' => $versi);
        $this->db->where('id_jenis', $id);
        $this->db->update('tb_jenis', $data);
    }

    function cari_like($kata) {
        $this->db->like('nama_jenis', $kata);
        $q = $this->db->get('tb_jenis');
        return $q;
    }

    function cari($kata, $limit, $offset) {
        $this->db->like('nama_jenis', $kata);
        $ambil = $this->db->get('tb_jenis', $limit, $offset);
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        }
    }
    //this for xml-rpc
    function ambil_jenis($jenis, $limit, $offset){
        $ambil = $this->db->get_where('tb_jenis', array('jenis_soal' => $jenis), $limit, $offset);
        if($ambil->num_rows() > 0){
            foreach ($ambil->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
    }

}

?>
